# SETWD ===========================
rm(list=ls())


# LIBRARIES ===========================

library(tidyverse)
library(foreign)
library(stargazer)
library(lmtest)
library(multiwayvcov)

# IMPORT DATA ===========================

dfgi <- read.csv("data/sve-08_04_24.csv",stringsAsFactors = F)

# AGE OF DEMOCRACY ===========================

temp <- dfgi%>%
  filter(!is.na(gini_disp),!is.na(democracy_duration))

## TAB A12. Logit: Age of Democracy ----

m4 <- glm(erosion.strict ~ gini_disp + democracy_duration, 
          data=temp, family=binomial)
crse4 <- coeftest(m4, cluster.vcov(m4, temp$country.name))
crse4

m5 <- glm(erosion.strict ~ gini_disp + log(gdppc) + democracy_duration, 
          data=temp, family=binomial)
crse5 <- coeftest(m5, cluster.vcov(m5, temp$country.name))
crse5

m6 <- glm(erosion.strict ~ gini_disp + log(gdppc) + year + democracy_duration, 
          data=temp, family=binomial)
crse6 <- coeftest(m6, cluster.vcov(m6, temp$country.name))
crse6

stargazer(crse4,crse5,crse6, title="Logit: Age of Democracy",digits=3,
          header=F,df=F,omit.stat=c("f","ser","aic","ll"),
          add.lines = list(c("Observations", nobs(crse4), nobs(crse5), nobs(crse6))),
          star.cutoffs=c(0.05,0.01,0.001),
          star.char=c("*","**","***"),
          notes="$^{*} p<0.05$; $^{**} p<0.01$; $^{***} p<0.001$",
          covariate.labels=c("Gini","Logged GDP per capita","Year",
                             "Age of Democracy"),
          notes.append=F,table.placement="h!",
          out="tables/apptab-age-10_27_24.tex")

## FIG A2. Outliers Test ----

temp <- dfgi%>%
  filter(!is.na(gini_disp))

e.countries <- c("Full Sample",unique(temp$country.name[temp$eroder==1]))

outlier.df4 <- data.frame(e.countries)

outlier.df4 <- outlier.df4%>%
  mutate(gini=NA,gini.p=NA,gini.se=NA,
         gdp=NA,gdp.p=NA,gdp.se=NA,
         age=NA,age.p=NA,age.se=NA)

for(i in 1:length(e.countries)){
  country.exclude <- e.countries[i]
  
  temp2 <- temp%>%
    filter(country.name!=country.exclude)
  
  m.exc <- glm(erosion.strict ~ gini_disp + log(gdppc) + year + 
                 democracy_duration, data=temp2, family=binomial)
  crse.exc <- coeftest(m.exc, cluster.vcov(m.exc, temp2$country.name))
  
  outlier.df4$gini[i] <- crse.exc[2,1]
  outlier.df4$gini.se[i] <- crse.exc[2,2]
  outlier.df4$gini.p[i] <- crse.exc[2,4]
  
  outlier.df4$gdp[i] <- crse.exc[3,1]
  outlier.df4$gdp.se[i] <- crse.exc[3,2]
  outlier.df4$gdp.p[i] <- crse.exc[3,4]
  
  outlier.df4$year[i] <- crse.exc[4,1]
  outlier.df4$year.se[i] <- crse.exc[4,2]
  outlier.df4$year.p[i] <- crse.exc[4,4]
  
  outlier.df4$age[i] <- crse.exc[5,1]
  outlier.df4$age.se[i] <- crse.exc[5,2]
  outlier.df4$age.p[i] <- crse.exc[5,4]
  
}

mean(outlier.df4$gini>0)
mean(outlier.df4$gini.p<0.05)
mean(outlier.df4$age.p<0.05)

outlier.df4 <- outlier.df4%>%
  mutate(group=(e.countries=="Full Sample"))%>%
  mutate(country=as.factor(e.countries))%>%
  mutate(country=relevel(country,"Full Sample"))


ggplot(outlier.df4,aes(x=gini,y=country,color=group))+
  geom_vline(xintercept=0)+
  geom_errorbarh(aes(xmin=gini-1.96*gini.se,
                     xmax=gini+1.96*gini.se))+
  geom_point()+
  ylab("Country Excluded")+
  xlab("Gini Effect")+
  scale_y_discrete(limits=rev)+
  scale_color_manual(values=c("gray60","black"),guide="none")+
  theme_bw()

ggsave(filename="figures/outliers-m4-gini-10_27_24.png",width=4,height=3.5)

ggplot(outlier.df4,aes(x=gdp,y=country,color=group))+
  geom_vline(xintercept=0)+
  geom_errorbarh(aes(xmin=gdp-1.96*gdp.se,
                     xmax=gdp+1.96*gdp.se))+
  geom_point()+
  ylab("Country Excluded")+
  xlab("Logged GDP Per Capita Effect")+
  scale_y_discrete(limits=rev)+
  scale_color_manual(values=c("gray60","black"),guide="none")+
  theme_bw()

ggsave(filename="figures/outliers-m4-gdp-10_27_24.png",width=4,height=3.5)

ggplot(outlier.df4,aes(x=year,y=country,color=group))+
  geom_vline(xintercept=0)+
  geom_errorbarh(aes(xmin=year-1.96*year.se,
                     xmax=year+1.96*year.se))+
  geom_point()+
  ylab("Country Excluded")+
  xlab("Year Effect")+
  scale_y_discrete(limits=rev)+
  scale_color_manual(values=c("gray60","black"),guide="none")+
  theme_bw()

ggsave(filename="figures/outliers-m4-year-10_27_24.png",width=4,height=3.5)

ggplot(outlier.df4,aes(x=age,y=country,color=group))+
  geom_vline(xintercept=0)+
  geom_errorbarh(aes(xmin=age-1.96*age.se,
                     xmax=age+1.96*age.se))+
  geom_point()+
  ylab("Country Excluded")+
  xlab("Age of Democracy Effect")+
  scale_y_discrete(limits=rev)+
  scale_color_manual(values=c("gray60","black"),guide="none")+
  theme_bw()

ggsave(filename="figures/outliers-m4-age-10_27_24.png",width=4,height=3.5)


## Descriptive Stats ----
# weak correlation between age of democracy and year:
temp <- dfgi%>%
  filter(!is.na(year),!is.na(democracy_duration))
cor(temp$year,temp$democracy_duration)

# oldest democracy - US:
temp%>%
  filter(democracy_duration==max(democracy_duration))%>%
  dplyr::select(country.name,democracy_duration)

# second oldest democracy - 48 years younger:
temp%>%
  filter(country.name!="United States")%>%
  filter(democracy_duration==max(democracy_duration))%>%
  dplyr::select(country.name,democracy_duration)




